<template>
  <div class="app-container">
    <div style="margin: 20px 0">
      <el-button plain @click="addroledialog">添加角色</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='角色名称'>
        <template slot-scope="scope">
          {{scope.$index}}
        </template>
      </el-table-column>
      <el-table-column label="角色Id" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.sex}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="success" v-if="scope.row.status!='true'" @click="qiyong(scope.row, 'true')" plain>启用</el-button>
          <el-button size="mini" type="warning" v-if="scope.row.status!='false'" @click="jinyong(scope.row, 'false')" plain>禁用</el-button>
          <el-button size="mini" type="primary" @click="bianji(scope.row,scope.$index)" plain>编辑</el-button>
          <el-button size="mini" type="danger" @click="shanchu" plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30,50]"
                     :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
    <!--分页END-->
    <!--添加角色弹窗-->
    <el-dialog title="添加角色" :visible.sync="addroleFormVisible">
      <el-form autoComplete="on" :model="dialogaddform" ref="dialogaddform" :rules="addrolesRules">
        <el-form-item label="角色名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="dialogaddform.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-select v-model="dialogaddform.sex" placeholder="请选择性别">
            <el-option label="男" value="man"></el-option>
            <el-option label="女" value="woman"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addroleFormVisible = false">取 消</el-button>
        <el-button type="primary" @click.native.prevent="addrole">确 定</el-button>
      </div>
    </el-dialog>
    <!--添加角色弹窗END-->
    <!--编辑表单弹窗-->
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="dialogdetailform">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="dialogdetailform.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select v-model="dialogdetailform.sex" placeholder="请选择性别">
            <el-option label="男" value="man"></el-option>
            <el-option label="女" value="woman"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑表单弹窗END-->
  </div>
</template>

<script>
  import { addroles, getList, qiyong, jinyong, getrolesDetail, sureEdit, shanchu } from '@/api/system/roleslist'

  export default {
    data() {
      return {
        list: null,
        listLoading: true,
        total: null,
        listQuery: {
          page: 1, // 分页当前页
          limit: 20, // 分页每页数量
          input1: undefined, // 搜索栏一
          type: undefined, // 搜索栏下拉选择
          sort: '+id'
        },
        addroleFormVisible: false,
        dialogaddform: {
          name: '',
          sex: ''
        },
        addrolesRules: {
          name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
          sex: [{ required: true, message: '请选择性别', trigger: 'change' }]
        },
        editdetail: null,
        dialogFormVisible: false,
        dialogdetailform: {
          name: '',
          sex: ''
        },
        formLabelWidth: '120px'
      }
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        }
        return statusMap[status]
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        this.listLoading = true
        var listQuery = { 'paramsid': '123' }
        getList(listQuery).then(response => {
          this.list = response.data.items
          this.total = response.data.total
          this.listLoading = false
        })
      },
      addroledialog() {
        this.addroleFormVisible = true
      },
      addrole() {
        this.$refs.dialogaddform.validate(valid => {
          if (valid) {
            addroles('123').then(response => {
              this.addroleFormVisible = false
            })
          } else {
            return false
          }
        })
      },
      qiyong(row, status) {
        this.listLoading = true
        var listQuery = { 'paramsid': '123', 'flag': true }
        qiyong(listQuery).then(response => {
          this.listLoading = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          row.status = status
        })
      },
      jinyong(row, status) {
        this.listLoading = true
        var listQuery = { 'paramsid': '321', 'flag': true }
        jinyong(listQuery).then(response => {
          this.listLoading = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          row.status = status
        })
      },
      bianji(row, _index) {
        var parameter = { 'paramsid': '321' }
        getrolesDetail(parameter).then(response => {
          this.dialogdetailform = response.data.items[0]
          this.dialogFormVisible = true
        })
      },
      sureEdit() {
        var submitinfo = {
          'paramsid': this.dialogdetailform.id,
          'name': this.dialogdetailform.name,
          'sex': this.dialogdetailform.sex }
        sureEdit(submitinfo).then(response => {
          this.dialogFormVisible = false
        })
      },
      shanchu() {
        this.listLoading = true
        shanchu('123').then(response => {
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.fetchData()
      }
    }
  }
</script>
